package com.boot.sample.jdbc.service.impl;

import com.boot.sample.jdbc.aop.Log;
import com.boot.sample.jdbc.domain.AccessLog;
import com.boot.sample.jdbc.service.AccessLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Service
public class AccessLogServiceImpl implements AccessLogService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Log
    @Override
    public AccessLog queryById(Long id) {
        String sql="SELECT * FROM access_log WHERE id = ?";

        List<AccessLog> list= jdbcTemplate.query(sql,new Object[]{id},new AccessLogMapper());
        if(null != list&&list.size()>0){
            return list.get(0);
        }
        return null;

    }


    class AccessLogMapper implements RowMapper<AccessLog> {

        @Override
        public AccessLog mapRow(ResultSet resultSet, int i) throws SQLException {
            AccessLog stu = new AccessLog();
            stu.setId(resultSet.getInt("id"));
            stu.setDescription(resultSet.getString("description"));
            return stu;
        }
    }
}
